let pay={
	prePay:function(data,reBackFunction,errorFunction){
		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', pay.onBridgeReady(data,reBackFunction,errorFunction), false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', pay.onBridgeReady(data,reBackFunction,errorFunction));
				document.attachEvent('onWeixinJSBridgeReady', pay.onBridgeReady(data,reBackFunction,errorFunction));
			}
		} else {
			pay.onBridgeReady(data,reBackFunction,errorFunction);
		}
	},
	onBridgeReady:function(params,reBackFunction,errorFunction){
				let user=uni.getStorageSync("user");
				if(params.user){
					user=params.user;
				}
				let ddlx00="1";
				let outTradeNo=params.out_trade_no;
				if(outTradeNo.indexOf("WXNAT")>-1||outTradeNo.indexOf("WXCLINIC")>-1){
					//WXNAT 核酸缴费 WXCLINIC 门诊处方支付
					ddlx00="2";
				}else if(outTradeNo.indexOf("WXPREPAY")>-1){
					ddlx00="3";
				}
				//自平台预下单				let myPrePayData={					"kh0000":user.kh0000,					"klx000":"3",					"xming0":user.xming0,					"mzh000":user.jzkh00,					"zffs00":"2",					"jyrq00":new Date(),//就诊日期					"ddh000":params.out_trade_no,					"ddje00":""+(parseFloat(params.amount.total)/100),					"ddlx00":ddlx00,//订单类型（1 挂号 2 门诊缴费 3 预交金充值）					"xtmc00":"3",					"sfzh00":user.kh0000,					"ddxq00":JSON.stringify(JSON.stringify(params)),					"patid0":user.patid0,					"hdurl0":"",					"funid":"F07.15.02.01"				};
				if(params.user){
					delete params.user;
				}
				//平台预下单				getApp().$postJson("/qnyesb/qnyesbproxy",myPrePayData,res=>{					if(res.state==0){
						//params.amount.total=(params.amount.total*100);
						//微信预下单
						getApp().$postJson2("/pay/jsapiPreOrder",params,result=>{
								if(result.state==200){
									let data=result.data;
									WeixinJSBridge.invoke(
										'getBrandWCPayRequest', {
											appId: data.appId, //公众号名称，由商户传入
											timeStamp: data.timeStamp, //时间戳，自1970年以来的秒数
											nonceStr: data.nonceStr, //随机串
											package: data.packages, //prepay_id用等式的格式
											signType: data.signType, //微信签名方式：
											paySign: data.paySign, //微信签名
										},
										function(res) {
											let prepayIdStr=data.packages;
											let prepayId=prepayIdStr.split("=")[1];
											params.prepayId=prepayId;//微信平台订单号
											if (res.err_msg == "get_brand_wcpay_request:ok") {
												//更新订单状态(支付成功)
												result.prepayId=prepayId;
												pay.updateOrder(params,"02",updateRes=>{
													reBackFunction(result)
												});
											} else {
												//更新订单状态(撤销支付)
												//updateOrder('4')
												pay.updateOrder(params,"03",updateRes=>{
													errorFunction(result)
												});
												
											}
										}
									);
								}else{
									uni.showToast({
										title:result.message,
										icon:"none",
										duration:5000
									})
								}
						})					}else{						uni.showToast({							title:"支付失败:"+res.message,							icon:"error",							duration:5000						})					}				})	},updateOrder(order,statu,fun){
		let updateData={
			"ddh000":order.out_trade_no,
			"ptddh0":order.prepayId,
			"ddzt00":statu,
			"funid":"F07.15.02.06"
		};
		getApp().$postJson("/qnyesb/qnyesbproxy",updateData,res=>{
			if(res.state==0){
				fun()
			}else{
				uni.showToast({
					title:"交易失败，平台会在1小时内自动给您退款，给您造成不便，敬请谅解",
					icon:"none",
					duration:5000
				})
			}
		})
	}
}

export default pay;

